package com.example.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SignUtils {

    private static final String SECRET_KEY = "PZ123";  // 密钥，可以从配置文件中读取

    /**
     * 生成签名
     * @param timestamp 请求的时间戳
     * @return 生成的签名
     * @throws NoSuchAlgorithmException
     */
    public static String generateSign(String timestamp) throws NoSuchAlgorithmException {
        String rawSign = SECRET_KEY + timestamp;
        return md5(md5(rawSign));
    }

    /**
     * MD5加密
     * @param input 输入字符串
     * @return MD5加密后的字符串
     * @throws NoSuchAlgorithmException
     */
    private static String md5(String input) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(input.getBytes());
        StringBuilder hexString = new StringBuilder();
        for (byte b : messageDigest) {
            hexString.append(String.format("%02x", b));
        }
        return hexString.toString();
    }
}
